import {GCtrl} from "../../../lib/util/GCtrl";
import FPJDefine from "../../../Game/FPJ/Prefabs/FPJDefine";

const {ccclass, property} = cc._decorator;
const Msg_Config = {
    Head_widget : 8,
    MsgBgWidget:78,
    NameWidget:35,
    EmojWidget:138,

    MsgBgScale:1,
    lbMsgScale:1,
    Left:cc.Label.HorizontalAlign.LEFT,
    Right:cc.Label.HorizontalAlign.RIGHT,
    Center:cc.Label.HorizontalAlign.CENTER,

    lbMsgPositionX:17.5,
}

@ccclass
export default class DWC_SimpleChatItem extends cc.Component {

    @property(cc.Sprite)
    emojSp:cc.Sprite = null;

    @property(cc.SpriteAtlas)
    emojSpAtlas:cc.SpriteAtlas = null;

    @property(cc.Widget)
    headWiget:cc.Widget = null;

    @property(cc.Widget)
    msgBgWiget:cc.Widget = null;

    @property(cc.Widget)
    lbNameWiget:cc.Widget = null;

    @property(cc.Widget)
    emojWiget:cc.Widget = null;

    @property(cc.Node)
    msgBg: cc.Node = null;

    @property(cc.Label)
    lbMsg:cc.Label = null;

    @property(cc.Label)
    lbName:cc.Label = null;

    @property(cc.Sprite)
    headIcon:cc.Sprite = null;

    @property(cc.Float)
    SpaceHeight = 40;

    @property(cc.Float)
    maxWidth = 100;

    _data = null;

    itemID = null;
    start () {

    }

    /**
     */
    setSelfUI(){


        let isSelf =  this._data["rid"] == cc.vv.session.getRid();

        //设置缩放
        this.msgBg.scaleX  = isSelf ? -Msg_Config.MsgBgScale : Msg_Config.MsgBgScale;
        this.lbMsg.node.scaleX = isSelf ? -Msg_Config.lbMsgScale : Msg_Config.lbMsgScale;
        //
        this.lbMsg.node.x = Msg_Config.lbMsgPositionX;
        this.lbMsg.node.anchorX = isSelf ? 1 : 0;


        //1头像widget
        this.headWiget.isAlignRight = isSelf ? true : false ;       //自己右对齐
        this.headWiget.isAlignLeft = isSelf ? false : true;         //别人左对齐
        //2头像对齐距离
        isSelf ? (this.headWiget.right = Msg_Config.Head_widget) : (this.headWiget.left = Msg_Config.Head_widget);      //自己右，别人左边

        //3聊天背景框widget
        this.msgBgWiget.isAlignRight = isSelf ? true : false;       //自己右对齐
        this.msgBgWiget.isAlignLeft = isSelf ? false : true;        //别人左对齐
        //4聊天框对齐距离
        isSelf ? (this.msgBgWiget.right = Msg_Config.MsgBgWidget) : (this.msgBgWiget.left = Msg_Config.MsgBgWidget);    //自己右，别人左边

        //聊天lable对齐
        if(isSelf && this.getMsgBgWidth() < this.maxWidth ){
            this.lbMsg.horizontalAlign = Msg_Config.Right;
        }else {
            this.lbMsg.horizontalAlign = Msg_Config.Left;
        }
        //this.lbMsg.horizontalAlign = isSelf ? Msg_Config.Right : Msg_Config.Left ; //别人左对齐，自己右对齐

        //名字widget
        // this.lbNameWiget.isAlignRight = isSelf ? true : false;           //自己右对齐
        // this.lbNameWiget.isAlignLeft = isSelf ? false : true;            //别人左对齐
        // isSelf ? (this.lbNameWiget.right = Msg_Config.NameWidget) : (this.lbNameWiget.left = Msg_Config.NameWidget);

        //名字label对齐
        //this.lbName.horizontalAlign = isSelf ? Msg_Config.Left : Msg_Config.Right;

        //聊天表情widget
        this.emojWiget.isAlignRight = isSelf ? true : false;
        this.emojWiget.isAlignLeft = isSelf ? false : true;
        isSelf ? (this.emojWiget.right = Msg_Config.EmojWidget) : (this.emojWiget.left = Msg_Config.EmojWidget);




    }

    init(itemID , data){
        this._data = data;
        this.itemID = itemID;
        this.initUI()
    }

    setTipUI(isTip){

        this.lbName.node.active = isTip ? false :true;
        this.emojSp.node.active = isTip ? false :true;
        this.headIcon.node.active = isTip ? false : true;

        if(isTip){
            this.msgBgWiget.isAlignRight = false
            this.msgBgWiget.isAlignLeft = false
            this.msgBg.x = 0;
            this.msgBg.anchorX = 0.5;

            this.lbMsg.node.anchorX = 0.5;
            this.lbMsg.horizontalAlign = Msg_Config.Center;
            this.lbMsg.node.x = 0;
            this.lbMsg.string = this.getTipContent();
        }else {
            this.lbMsg.node.anchorX = 0;
            this.msgBg.anchorX =0 ;

            this.lbMsg.node.anchorX = 0;
            this.lbMsg.horizontalAlign = Msg_Config.Left;
            this.lbMsg.node.x = Msg_Config.lbMsgPositionX;
        }
    }
    initUI(){
        this.setSelfUI();
        let isTip = this._data["tip"];
        // if(isTip){
        //     //this.setTipUI(true);
        // }else {
        //     // this.setTipUI(false);
        //     // this.initContent();
        //     // this.lbName.string = GCtrl.visibleStr(this._data["rolename"] + "", 6);
        //     // //设置头像
        //     // GCtrl.setHeadSpr(this._data["logo"], this.headIcon);
        //     //
        //     // this.adapterHeightWidth();
        // }
        //this.setTipUI(false);
        this.initContent();
        this.lbName.string = GCtrl.visibleStr(this._data["rolename"] + "", 6);
        //设置头像
        GCtrl.setHeadSpr(this._data["logo"], this.headIcon);
        this.adapterHeightWidth();
        if(isTip){
            this.lbMsg.string = this.getTipContent();
        }
    }

    initContent(){
        let str = this._data["content"];
        if( str.search("@emoticon_") !== -1 ){
            let strSlice = str.slice(10)
            let spf = this.emojSpAtlas.getSpriteFrame(strSlice);
            if(spf){
                this.emojSp.spriteFrame = spf;
                this.emojSp.node.active = true;
                this.lbMsg.string ="";
            }else {
                this.lbMsg.string =this.getSliceStr(str);
                this.emojSp.node.active = false;
            }
        }else {
            this.lbMsg.string =this.getSliceStr(str);
            this.emojSp.node.active = false;
        }
    }

    getSliceStr(str){
        let maxCount = Math.floor(this.maxWidth / this.lbMsg.fontSize );
        if(str.length > maxCount){
            str = str.slice(0,maxCount-1-3);
            str = str +  "...";
        }
        return str;
    }


    getMsgBgWidth(){
        return 30 + (this.lbMsg.fontSize * this.getStringLength(this._data["content"])  > this.maxWidth ? this.maxWidth : this.lbMsg.fontSize * this.getStringLength(this._data["content"]) );
    }

    adapterHeightWidth(){
        this.msgBg.width = this.getMsgBgWidth();
        setTimeout(()=>{
            this.msgBg.getComponent(cc.Layout).updateLayout();
            this.node.height = this.getMsgItemHeight(this.lbMsg.string);
        },0)
    }

    getStringLength(str){
        if( str.search("@emoticon_") !== -1 ){
            let strSlice = str.slice(10)
            let spf = this.emojSpAtlas.getSpriteFrame(strSlice);
            if(spf){
                return 2;
            }
        }
        return str.length;
    }

    //获取整个item的高度
    getMsgItemHeight(data){
        let str = data ;
        let msgLength = this.lbMsg.fontSize * str.length;
        let msgHangShu = 1 ;// Math.ceil( msgLength / this.maxWidth );      //这里不需要自动高度了
        let height = this.SpaceHeight + this.lbMsg.lineHeight * msgHangShu;
        return height;
    }

    getTipContent(){
        let tip = `${this._data["rolename"]}进入游戏`;
        return tip;
    }
}
